// result.ts
import { apis } from '../../apis/apis'
// import { request } from '../../utils/request'
import { requestPromise } from '../../utils/requestPromise';
import { isEmptyObject } from '../../utils/util'
Component({

  data: {
    title: "",
    answerGroup: [],
    detail: {},
    eWidth: '0',
    sWidth: '0',
    tWidth: '0',
    jWidth: '0',
  },
  methods: {
    adLoad() {
      console.log('Banner 广告加载成功')
    },
    adError(err:any) {
      console.error('Banner 广告加载失败', err)
    },
    adClose() {
      console.log('Banner 广告关闭')
    },
    async getDetail() {
      let _th = this;
      let app = getApp();
      if(isEmptyObject(app.globalData.detail)){
        console.log('没有global data')
        requestPromise({
          url: apis.getDetail,
          method: "GET",
          data: {},
        }).then((res:any)=>{
          wx.setStorageSync('detail', res.data.data.detail);
          _th.setData({
            detail: res.data.data.detail,
          });
        })
      }else{
        console.log('有global data')
        _th.setData({
          detail: app.globalData.detail,
        });
      }
    },
    backToHome() {
      wx.switchTab({
        url: '/pages/main/main',
      })
    },
    onShareAppMessage() {
      return {
        title: "我的MBTI测试结果",
        path: "/pages/browse/browse?title=" + this.data.title,
      }
    },
    onShareTimeline() {
      return {
        title: "我的MBTI测试结果",
        query: "from=timeline&title=" + this.data.title + "&e=" + this.data.eWidth + "&s=" + this.data.sWidth + "&t=" + this.data.tWidth + "&j=" + this.data.jWidth,
      }
    },
    async onLoad(option: any) {
      if (option.from == 'timeline') {
        this.initDataFromTimeLine(option)
      } else {
        let result = {}
        if (option.history == undefined) {
          result = wx.getStorageSync('result');
        } else {
          let history = wx.getStorageSync('history');
          if (isEmptyObject(history) === true) {
            wx.showToast({
              title: "没有测试记录！",
              icon: 'error',
            })
          }
          result = history[option.history];
        }
        this.initDataFromTesting(result);
      }
      await this.getDetail();
    },
    initDataFromTimeLine(option:any){
      this.setData({
        title: option.title,
        // answerGroup:res.answerGroup,
        eWidth: option.e,
        sWidth: option.s,
        tWidth: option.t,
        jWidth: option.j,
      })
    },
    initDataFromTesting(result: any) {
      // console.log(result);
      let eWidth = parseFloat((result.answerGroup[0].E / (result.answerGroup[0].E + result.answerGroup[0].I)).toFixed(2)) * 100;
      let sWidth = parseFloat((result.answerGroup[1].S / (result.answerGroup[1].S + result.answerGroup[1].N)).toFixed(2)) * 100;
      let tWidth = parseFloat((result.answerGroup[2].T / (result.answerGroup[2].T + result.answerGroup[2].F)).toFixed(2)) * 100;
      let jWidth = parseFloat((result.answerGroup[3].J / (result.answerGroup[3].J + result.answerGroup[3].P)).toFixed(2)) * 100;
      this.setData({
        title: result.title,
        // answerGroup:res.answerGroup,
        eWidth: eWidth.toFixed(2),
        sWidth: sWidth.toFixed(2),
        tWidth: tWidth.toFixed(2),
        jWidth: jWidth.toFixed(2),
      })
    }
  },
  lifetimes: {
    attached() {
    },
  },
})
